package com.yimixian.app.adapter;

import android.app.Activity;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;
import com.yimixian.app.R;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.Constants;
import com.yimixian.app.model.SortGoodsItem;
import com.yimixian.app.ui.HomeItemView;

import java.util.List;

public class CouDanResultAdapter extends BaseAdapter {

    public interface onItemViewClickListener {
        void onItemViewClick(View view, int sortPosition, int ChildPositin, Point startPoint);
    }

    private List<SortGoodsItem> mList;
    private Activity mActivity;
    private LayoutInflater mInflater;
    private Picasso picasso;
    private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;
    private onItemViewClickListener mListener;
    private boolean isFromCouDanPage;

    public CouDanResultAdapter(Activity activity, List<SortGoodsItem> mList,
                               GoodsItemPresenter.OnPlusButtonClickListener plusButtonClickListener) {
        mActivity = activity;
        this.mList = mList;
        this.mOnPlusButtonClickListener = plusButtonClickListener;
        mInflater = LayoutInflater.from(activity);
        picasso = Picasso.with(mActivity);
    }

    public void setList(List<SortGoodsItem> mList) {
        if(mList == null) {
            return ;
        }
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void clearList() {
        if(mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public SortGoodsItem getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.search_result_item, parent, false);
            holder = new ViewHolder();
            holder.itemView = convertView;
            holder.linBottom = (LinearLayout) convertView.findViewById(R.id.lin_bottom);
            holder.leftItem = (HomeItemView) convertView.findViewById(R.id.left_item);
            holder.rightItem = (HomeItemView) convertView.findViewById(R.id.right_item);
            holder.bottomLine = convertView.findViewById(R.id.bottom_line);
            holder.leftItem.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
            holder.rightItem.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        initData(holder, position);
        return convertView;
    }

    public class ViewHolder {
        public View itemView;
        public LinearLayout linBottom;
        public HomeItemView leftItem, rightItem;
        public View bottomLine;
    }

    // 初始化数据
    public void initData(ViewHolder holder, final int position) {
        if (position >= mList.size()){
            return;
        }
        SortGoodsItem sortGoodsItem = mList.get(position);
        //是否显示底线
        if(mList.size() > position + 1 &&
                mList.get(position + 1).type == SortGoodsItem.SECTION){
            holder.bottomLine.setVisibility(View.VISIBLE);
        }else {
            holder.bottomLine.setVisibility(View.GONE);
        }
        setItem(sortGoodsItem, holder);
    }

    /**
     * 分类下面的item
     * @param sortGoodsItem
     * @param holder
     */
    void setItem(SortGoodsItem sortGoodsItem, ViewHolder holder){
        holder.linBottom.setVisibility(View.VISIBLE);
        holder.leftItem.bindData(sortGoodsItem.leftGoodsItem, Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST);
        holder.rightItem.bindData(sortGoodsItem.RightGoodsItem, Constants.STATE_COUDANFRAGMENT_BUTTON_BROADCAST);
        holder.leftItem.setIsFromCouDanPage(isFromCouDanPage);
        holder.rightItem.setIsFromCouDanPage(isFromCouDanPage);
    }

    public void setIsFromCouDanPage(boolean isFromCouDanPage) {
        this.isFromCouDanPage = isFromCouDanPage;
    }
}
